#include "AnaDef.h"
#include "AnaAliDef.h"
#include "AliTRDdEdxUtils.h"

void THnMemCheck(const Int_t ntrack)
{
  TList *list = new TList;

  Int_t id = 0;

  AliSysInfo::AddStamp(Form("step%d",id),id); id++; 

  AliTRDdEdxUtils::IniCalibHist(list, kTRUE);
  AliSysInfo::AddStamp(Form("step%d",id),id); id++;

  THnF * hcalib = AliTRDdEdxUtils::GetHistPHQ(0, 1, 1);

  for(Int_t ii=0; ii<ntrack; ii++){
    const Int_t ncls = 150;
    TVectorD arrayQ(200), arrayX(200);
    for(Int_t ic = 0; ic<ncls; ic++){
      arrayQ[ic] = gRandom->Landau(2, 0.5);
      arrayX[ic] = gRandom->Rndm()*16740;
    }
    AliTRDdEdxUtils::FillCalibHist(ncls, &arrayQ, &arrayX, hcalib, 1);
    if(ii%1000==0){
      printf("Filling %d, step %d\n", ii, id);
      AliSysInfo::AddStamp(Form("step%d",id),id); id++;
    }
  }

  TFile *fout=new TFile("histout.root","recreate");
  list->Write();
  fout->Save();
  fout->Close();

  AliTRDdEdxUtils::DeleteCalibHist();
  printf("last step destructor %d\n", id);
  AliSysInfo::AddStamp(Form("step%d",id),id); id++;

  TTree * tree = AliSysInfo::MakeTree("syswatch.log");
  //tree->Draw("VM:Entry$","","*");
  tree->SetName("tree");
  fout=new TFile("watch.root","recreate");
  tree->Write();
  fout->Save();
  fout->Close();
}

int main(int argc, char * argv[])
{
  for(int ii=0; ii<argc; ii++){
    printf("%d: %s\n", ii, argv[ii]);
  }

  THnMemCheck(atoi(argv[1]));
  return 0;
}
